﻿
package extremefx.collections {
	import extremefx.IComparer;
	import extremefx.IComparable;	
	/**
	 * @author Marcelo Volmaro
	 */
	internal final class Comparer implements IComparer {
		public function compare(o1:*, o2:*):int {
			if (o1 === o2) {
				return 0;
				
			} else if (o1 == null){
				return -1;
				
			} else if (o2 == null) {
				return 1;
			}
			
			//Implements IComparable?
			if (o1 is IComparable){
				return IComparable(o1).compareTo(o2);
				
			} else if (o2 is IComparable){
				return -IComparable(o2).compareTo(o1);
			}
			
			//Are both numbers?
			if (o1 is Number && o2 is Number){
				if (o1 == o2){
					return 0;
					
				} else if (o1 < o2){
					return -1;
					
				} else {
					return 1;
				}
			}
			
			//Convert to strings and compare...
			var s1:String = o1.toString();
			var s2:String = o2.toString();
			
			if (s1 == s2){
				return 0;
				
			} else if (s1 < s2){
				return -1;
				
			} else {
				return 1;
			}
		}
	}
}
